/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.m010.ui.views;

import com.vaadin.data.Container;
import com.vaadin.ui.Component;
import com.vaadin.ui.Field;
import com.vaadin.ui.Table;
import com.vaadin.ui.TableFieldFactory;
import ep.libs.implementations.AbstractModule;
import ep.m001.ui.views.LoginView;
import ep.m002.module.M002Module;
import ep.m005.ui.views.M005View;
import ep.m010.ui.controllers.M010Controller;

/**
 *
 * @author atsi
 */
public class M010View extends M005View {

    private M010Controller controller;

    public M010View(AbstractModule module) {
        super(module);
    }

    public M010View(AbstractModule module, Object[] args) {
        super(module, args);
    }

    @Override
    protected void initializeFormStrings() {
        super.initializeFormStrings();
        dosageStrings = new String[]{
                    "prescribed",
                    "t009Medicine",
                    "quantity",
                    "wayOfInput",
                    "t013Frequency",
                    "duration"
                };

        dosageHeaders = new String[]{
                    CAPTION_PRESCRIBED,
                    CAPTION_MEDICINE,
                    CAPTION_QUANTITY,
                    CAPTION_WAY_OF_INPUT,
                    CAPTION_FREQUENCY,
                    CAPTION_DURATION
                };
    }

    @Override
    protected void initilizeComponents() {
        super.initilizeComponents();
        controller = new M010Controller(patientObject, module);
        prescForm.setReadOnly(true);
        dosageEditableTable.getRemoveButton().setVisible(false);
        dosageEditableTable.getAddButton().setVisible(false);

        dosageEditableTable.getTable().addGeneratedColumn("price", new Table.ColumnGenerator() {

            @Override
            public Object generateCell(Table source, Object itemId, Object columnId) {
                return controller.getPriceFromDosageObject(itemId);
            }
        });
    }

    @Override
    protected TableFieldFactory initializeTableFieldFactory() {
        final Table temp = new Table();
        TableFieldFactory newTableFieldFactory = new TableFieldFactory() {

            @Override
            public Field createField(Container container, Object itemId, Object propertyId, Component uiContext) {
                Field f = temp.getTableFieldFactory().createField(container, itemId, propertyId, uiContext);
                if (propertyId.equals("prescribed")) {
                    f.setCaption(M005View.CAPTION_PRESCRIBED);
                }else{
                    f.setReadOnly(true);
                }
                return f;
            }
        };

        return newTableFieldFactory;
    }

    @Override
    protected void changeModule() {
        module.getApplication().changeModule(M002Module.class, new Object[]{LoginView.PHARMACIST_TYPE, patientObject});
    }
}
